package mysql

import (
	"fmt"
	"go_wep_demo/settings"

	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

var db *sqlx.DB

func Init() (err error) {
	dbConf := settings.Config.DbConfig
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True", dbConf.User, dbConf.Password, dbConf.Host, dbConf.Port, dbConf.DbName)
	db, err = sqlx.Connect("mysql", dsn) //这里其实就是database/sql的open加ping
	if err != nil {
		return err
	}
	db.SetMaxOpenConns(dbConf.MaxOpenConns)
	db.SetMaxIdleConns(dbConf.MaxIdleConns)
	return nil
}

func Close() {
	_ = db.Close()
}
